package com.atguigu.dao.impl;

import com.atguigu.dao.BaseDao;
import com.atguigu.dao.OrderItemDao;
import com.atguigu.domain.OrderItem;

import java.util.Arrays;
import java.util.List;

/**
 * @author dl
 * @date 2020/5/4 13:39
 */
public class OrderItemDaoImpl extends BaseDao<OrderItem> implements OrderItemDao {
    @Override
    public List<OrderItem> listOrderItems(String id) {
        String sql = "select * from bs_order_item where id = ?";
        return getBeanList(sql,id);
    }

    @Override
    public int saveOrderItem(OrderItem orderItem) {
        String sql = "insert into bs_order_item(name,price,total_money,count,order_id) values(?,?,?,?,?)";
        return update(sql,orderItem.getName(),orderItem.getPrice(),orderItem.getTotalMoney(),orderItem.getCount(),orderItem.getOrderId());
    }

    @Override
    public void batchOrderItem(List<OrderItem> list) {
        String sql = "insert into bs_order_item(name,price,total_money,count,order_id) values(?,?,?,?,?)";
        Object[][] params = new Object[list.size()][5];
        // TODO why No value specified for parameter 2 Query
        // catch?????
        for (int i = 0; i < list.size() ; i++) {
            OrderItem orderItem = list.get(i);
            params[i] = new Object[]{orderItem.getName(),orderItem.getPrice(),orderItem.getTotalMoney(),orderItem.getCount(),orderItem.getOrderId()};
        }
        batchSave(sql,params);
    }
}
